package com.jason.apps.dashboard;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.apps.essentials.GetMapData;
import com.apps.realtyportal.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class ViewMap extends MapActivity{

	private Button btnsave;
	private Button btncancel;
	
	private MapView mapView;
	private MapController mapController = null;
	Drawable drawable;
	int lat = 0;
	int lon = 0;
	ArrayList listoflat = new ArrayList();
	ArrayList listoflon = new ArrayList();
	// Handles Taps on the Google Map 
	Handler h = new Handler(){
		
		// Invoked by the method onTap() 
		// in the class CurrentLocationOverlay
		@Override
		public void handleMessage(Message msg) {			
			Bundle data = msg.getData();
			
			// Getting the Latitude of the location
			int latitude = data.getInt("latitude");
			
			// Getting the Longitude of the location
			int longitude = data.getInt("longitude");	
		    listoflat.add(latitude);
		    listoflon.add(longitude);
			// Show the location in the Google Map
			showLocation(latitude,longitude);
			
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewmap);
        // Getting reference to map_view available in activity_main.xml
        mapView = (MapView) findViewById(R.id.mv);
        btnsave = (Button) findViewById(R.id.btnSave);
        btncancel = (Button) findViewById(R.id.btnCancel);
        
       
        mapView.setStreetView(true);
    	// Getting the MapController
    	mapController = mapView.getController();

        // Getting reference to tv_location available in activity_main.xml
        //tvLocation = (TextView) findViewById(Rt.id.tv_location);
        
        // Default Latitude
        int latitude =  10315888;
   
        // Default Longitude
    	int longitude = 123886557;
    	mapController.animateTo(new GeoPoint(latitude, longitude));
    	mapController.setZoom(17);
    	// Show the location in the Google Map
        showLocation(latitude,longitude);
        
        
        btnsave.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				backToManageProperty(lat, lon);
			}
		});
        
        btncancel.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
    }
    

	
private void showLocation(int latitude, int longitude){    
    	// Getting Overlays of the map
    	List<Overlay> overlays = mapView.getOverlays();
       
    	// Getting Drawable object corresponding to a resource image
    	Drawable drawable = getResources().getDrawable(R.drawable.mark_red);
       
    	// Creating an ItemizedOverlay
    	GetMapData overlaytap = new GetMapData(drawable,h);        
      
    	// Creating an instance of GeoPoint, to display in Google Map
    	GeoPoint p = new GeoPoint(
	    					latitude, 
	    					longitude
    					);
    	Toast.makeText(getApplicationContext(), "data"+latitude+""+longitude, Toast.LENGTH_LONG).show();
    	// Locating the point in the Google Map
    	mapController.animateTo(p);           
    	// Creating an OverlayItem to mark the point
    	OverlayItem overlayItem = new OverlayItem(p, "Item", "Item");
       
    	// Adding the OverlayItem in the LocationOverlay
    	overlaytap.addOverlay(overlayItem);
    	//backToManageProperty();
    	// Clearing the overlays
//    	overlays.clear();
       
    	// Adding locationOverlay to the overlay
    	overlays.add(overlaytap);
       
    	// Redraws the map
    	mapView.invalidate();
    
    
    }

	public void backToManageProperty(int lats, int lons){
		String strlat = listoflat.get(listoflat.size()-1).toString();
		String strlon = listoflon.get(listoflon.size()-1).toString();
		
		Toast.makeText(getApplicationContext(), "data"+strlat+""+strlon, Toast.LENGTH_LONG).show();
		Intent backtomanageProperty = new Intent(getApplicationContext(), ManageProperty.class);
		   	   backtomanageProperty.putExtra("latitude", strlat);	
		   	   backtomanageProperty.putExtra("longitude", strlon);
		   	   
		   	   setResult(RESULT_OK,backtomanageProperty);
		   	   finish();
		//startActivity(backtomanageProperty);
		listoflat.clear();
		listoflon.clear();

	}
	
	
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}	
}
